<template>
  <div>
    <div style="display: flex;justify-content: flex-end;margin: 5px 0">
      <el-button size="small" type="primary" style="margin-right: 70px" @click="addSonMenu(0)">添加一级菜单</el-button>
    </div>
    <el-table :data="menuData" style="width: 100%;margin-bottom: 20px;" row-key="id" border
              :tree-props="{children: 'childMenu', hasChildren: 'hasChildren'}">
      <el-table-column prop="menuName" label="菜单名称" min-width="200"></el-table-column>
      <el-table-column prop="menuIcon" label="图标" min-width="200"></el-table-column>
      <el-table-column prop="sorts" label="排序" min-width="200"></el-table-column>
      <el-table-column label="操作" min-width="200" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="primary" @click="addSonMenu(scope.row.id)">添加子菜单</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="菜单编辑" :visible.sync="dialog" width="600px" @close="closeDialog">
      <el-form :model="menuForm" ref="menuForm">
        <el-form-item v-show="false" label="id" label-width="80px">
          <el-input v-model="menuForm.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单名称" label-width="80px" prop="menuName"
                      :rules="[{required:true,message:'请输入菜单名称',trigger:'blur'}]">
          <el-input v-model.trim="menuForm.menuName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单图标" label-width="80px">
          <el-input v-model="menuForm.menuIcon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单路劲" label-width="80px">
          <el-input v-model="menuForm.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单排序" label-width="80px" prop="sorts"
                      :rules="[{required:true,message:'请输入排序',trigger:'blur'},{type:'number',message:'排序必须为数字类型'}]">
          <el-input v-model.number="menuForm.sorts" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新增菜单" :visible.sync="dialogAdd" width="600px" @close="closeDialogAdd">
      <el-form :model="AddForm" ref="AddForm">

        <el-form-item label="菜单名称" label-width="80px" prop="menuName"
                      :rules="[{required:true,message:'请输入菜单名称',trigger:'blur'}]">
          <el-input v-model.trim="AddForm.menuName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单图标" label-width="80px">
          <el-input v-model="AddForm.menuIcon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单路劲" label-width="80px">
          <el-input v-model="AddForm.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单排序" label-width="80px" prop="sorts"
                      :rules="[{required:true,message:'请输入排序',trigger:'blur'},{type:'number',message:'排序必须为数字类型'}]">
          <el-input v-model.number="AddForm.sorts" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="closeDialogAdd">取 消</el-button>
        <el-button type="primary" @click="submitAddForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "MenuIndex",
  data() {
    return {
      menuData: [],
      dialog: false,
      menuForm: {
        id: '',
        menuName: '',
        menuIcon: '',
        path: '',
        sorts: ''
      },
      dialogAdd: false,
      AddForm: {
        menuName: '',
        menuIcon: '',
        path: '',
        parentId: '',
        sorts: '',
      }
    }
  }, methods: {
    addSonMenu(parentId) {
      this.dialogAdd = true;
      this.AddForm.parentId = parentId;
    },
    submitAddForm() {
      this.$refs['AddForm'].validate(valid => {
        if (valid) {
          this.$axios.post('/menu/addMenu', this.$qs.stringify(this.AddForm)).then(res => {
            if (res.code === 200) {
              this.$message({type: 'success', message: '添加成功!'});
              this.closeDialogAdd();
              this.getData();
            }
          });
        }
      });
    },
    handleEdit(row) {
      this.menuForm.id = row.id;
      this.menuForm.menuName = row.menuName;
      this.menuForm.menuIcon = row.menuIcon;
      this.menuForm.path = row.path;
      this.menuForm.sorts = row.sorts;
      this.dialog = true;
    },
    handleDelete(index, row) {
      this.$confirm('确认删除?').then(() => {
        this.$axios.post('/menu/deleteMenu', this.$qs.stringify({id: row.id})).then(res => {
          if (res.code === 200) {
            this.$message({type: 'success', duration: 1000, message: '删除成功!'});
            this.getData();
          }
        });
      }).catch(() => {
      });
    },
    submit() {
      this.$refs['menuForm'].validate(valid => {
        if (valid) {
          this.$axios.post('/menu/editMenu', this.$qs.stringify(this.menuForm)).then(res => {
            if (res.code === 200) {
              this.$message({type: 'success', duration: 1000, message: '修改成功!'});
              this.getData();
              this.closeDialog();
            }
          })
        }
      });
    },
    closeDialog() {
      this.dialog = false;
      this.$refs['menuForm'].resetFields();
    },
    closeDialogAdd() {
      this.dialogAdd = false;
      Object.keys(this.AddForm).forEach(key => {
        this.AddForm[key] = '';
      });
      this.$refs['AddForm'].resetFields();//清除验证
    },
    getData() {
      this.$axios.post('/menu/getAllMenu').then(res => {
        if (res.code === 200) {
          this.menuData = res.data;
        }
      });
    }
  }, created() {
    this.getData();
  }
}
</script>

<style scoped>

</style>
